// mobile bind
// ============

define(['zepto', 'sui', 'dropload', 'zData', 'jrange'], function($){
  return {
    init: function(data) {
      if(location.href.indexOf("#reloaded")==-1){
        location.href=location.href+"#reloaded";
        location.reload();
      }
      
      this.initScroll();
      
//      this.$bindButton = $('[href="#edit-royalties"]');
//      this.$bindButton.length ? this.bindEditRoyalties() : void 0;
    },
    
    initScroll: function() {
        var page = 0;
        var size = 5;
        $('.list-content').dropload({
            scrollArea : $(".profile-wallet-content"),
            autoLoad: false,
            loadDownFn : function(me){
                page++;
                var result = '';
                $.ajax({
                    type: 'GET',
                    url: 'http://ons.me/tools/dropload/json.php?page='+page+'&size='+size,
                    dataType: 'json',
                    success: function(data){
                        var arrLen = data.length;
                        if(arrLen > 0){
                            for(var i=0; i<arrLen; i++){
                                result += '<div class="item-content">'
			              		          + '<div class="item-inner">'
			        			            + '<div class="col-50 detail">'
			        			              + ' <p class="no-margin">租车订单： 0123456789</p>'
			        			                + '<p class="no-margin"> 2012-12-12</p>'
			        			              + '</div>'
			        			              + '<div class="col-50 royalty">'
			        			                + '<div class="color-red price">-123.00</div>'
			        			              + '</div>'
			        			            + '</div>'
			        		              + '</div>'
                            }
                        // 如果没有数据
                        }else{
                            // 锁定
                            me.lock();
                            // 无数据
                            me.noData();
                        }
                        setTimeout(function(){
                            $('.list-container').append(result);
                            // 每次数据插入，必须重置
                            me.resetload();
                        },1000);
                    },
                    error: function(xhr, type){
                        alert('Ajax error!');
                        me.resetload();
                    }
                });
            }
        });
    },
    
    bindEditRoyalties: function() {
    	var that = this

        this.$bindButton.on('click', function(e) {
          e.preventDefault();
          var $page = '<div class="page">'
		        	  + '<header class="bar bar-nav fita-header">'
			  		    + '<a href="javascript:history.back(-1);" class="icon icon-left pull-left"></a>'
			  		    + '<a class="icon icon-menu pull-right open-panel"></a>'
			  		    + '<h1 class="title">修改提成率</h1>'
			  		  + '</header>'
        	  		  + '<div class="content profile-recruit-content">'
        	  		    + '<h5 style="margin: .75rem 0 .5rem .75rem;">招募规则：</h5>'
    	  		        + '<div class="rule">'
    	  		          + '<p class="no-margin">2.您可设置招募到的FITA的提成率（1%~10%），该FITA的所见的价格即为您所见价格的上浮若干百分比。</p>'
    	  	            + '</div>'
        	  	        + '<div class="range-wrap">'
        	  	          + '<span style="font-size: .65rem;">设置提成率：</span>'
        	  	          + '<span class="iconfont iconfont-minus"></span>'
        	  	          + '<input class="single-slider" type="hidden" value="0.0"/>'
        	  	          + '<span class="iconfont iconfont-plus"></span>'
        	  	        + '</div>'
        	  	        + '<div style="margin: 2rem .5rem 0">'
        	  	          + '<a href="" class="button create-button">生成招募海报</a>'
        	  	        + '</div>'
        	  	      + '</div>'
            
            that.bindPageEvent($($page));
            $.push.transitionIn($($page));
          
        });
    },
    
    bindPageEvent: function($page) {
        var that = this;
        that.initRange($page);
    },
    
    initRange: function($page) {
    	var $element = $page.find(".single-slider")
    	$element.jRange({
            from: 1,
            to: 10,
            step: 1,
            format: function(value, pointer) {
              return (Math.round(value) + "%");
            },
            width: 200,
            theme: 'theme-blue',
            showLabels: true,
            snap: true
        });
        
        $(document).on("click", ".iconfont-minus", function() {
      	var recruitVal = Number($(".single-slider").val()) < 1 ? '1' : Number($(".single-slider").val()) - 1  
          $('.single-slider').jRange('setValue', recruitVal.toFixed(2));
        })
        
        $(document).on("click", ".iconfont-plus", function() {
      	
      	var recruitVal = Number($(".single-slider").val()) > 10 ? '10' : Number($(".single-slider").val()) + 1  
      	$('.single-slider').jRange('setValue', recruitVal.toFixed(2));
        })
      	  
      }
  }
})